﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data;

namespace dzs.Web.Outstorage
{
    public partial class ListOutStorageByVen : Web.Admin.BasePage
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                if (Session["out_storageList_id"] != null)
                {
                    int out_storageList_id = Convert.ToInt32(Session["out_storageList_id"]);

                    BindGV(out_storageList_id);

                }
            }
        }

        private void BindGV(int out_storageList_id)
        {
            
            dzs.BLL.out_storageList olm = new BLL.out_storageList ();
            dzs.BLL.out_storage om = new BLL.out_storage();

            dzs.Model.out_storageList olist = olm.GetModel(out_storageList_id);//出库申请清单

            DataSet ds = olm.GetOutStorageListDetail(" mid= "+olist.material_id);//库存清单

            this.GridView1.DataSource = ds;
            this.GridView1.DataBind();

            DataSet ds_out = om.GetList(" material_id=" + olist.material_id + " and out_storageList_id=" + out_storageList_id);//已出库清单
           
            decimal alreadyCount = 0;//已出库量
            if (ds_out.Tables[0].Rows.Count > 0)
            {
                foreach (DataRow row in ds_out.Tables[0].Rows)
                {
                    string out_count = row["out_storage_count"].ToString();
                    alreadyCount += Convert.ToDecimal(out_count);//已出库量
                }
            }

            decimal out_storage_count = olist.out_storage_count.Value;//申请出库量

            this.lbl_mustCount.Text = out_storage_count.ToString();
            this.lbl_alreadyCount.Text = alreadyCount.ToString();  
            this.lbl_notCount.Text = (out_storage_count - alreadyCount).ToString();//未出库量
            this.lbl_receive_address.Text = olist.receive_address;
            this.lbl_receive_userName.Text = olist.receive_userName;
            this.lbl_receive_userPhone.Text = olist.receive_userPhone;
            this.lbl_mid.Text = olist.material_id.ToString();
            this.lbl_targetProject.Text = olist.target_project;
            

            string targetProid = this.lbl_targetProject.Text;

            if (!string.IsNullOrEmpty(targetProid))
            {
                DataSet ds_targetProject = new Maticsoft.BLL.project().GetList(" proid='" + targetProid + "' ");

                if (ds_targetProject.Tables[0].Rows.Count == 1)
                {
                    this.lbl_targetProjectText.Text = ds_targetProject.Tables[0].Rows[0]["projectName"].ToString();
                }
            }
            else
            {
                this.lbl_targetProjectText.Text = "无";
            }

            this.lbl_out_storageList_id.Text = out_storageList_id.ToString();//预出库清单
  
        }

        protected void GridView1_RowCommand(object sender, GridViewCommandEventArgs e)
        {
            if (e.CommandName == "lbn_out")
            {
                if (e.CommandArgument != null)
                {

                    int inid = Convert.ToInt32(e.CommandArgument.ToString());//库存ＩＤ

                    string receive_address = lbl_receive_address.Text;//收货地点
                    string receive_userName = lbl_receive_userName.Text;//接收人
                    string receive_userPhone = lbl_receive_userPhone.Text;//接收人电话
                    string mids = lbl_mid.Text;//物料ID
                    string out_storageList_ids = lbl_out_storageList_id.Text;//预出库清单


                    decimal out_count = 0;
                    decimal real_count = 0;
                    int mid = 0;
                    int spid = 0;
                    int out_storage_User_id = 0;
                    int out_storageList_id = 0;

                    if (Session["login"] != null)
                    {
                        DataSet ds = Session["login"] as DataSet;

                        out_storage_User_id = Convert.ToInt32(ds.Tables[0].Rows[0]["user_id"].ToString());

                        GridViewRow drv = ((GridViewRow)(((LinkButton)(e.CommandSource)).Parent.Parent));

                        TextBox txt_out_storage_count = drv.FindControl("txt_out_storage_count") as TextBox;//出库数量
                        TextBox txt_out_storage_Time = drv.FindControl("txt_out_storage_Time") as TextBox;//出库时间
                        Label lbl_realCount = drv.FindControl("lbl_venNums") as Label;//库存数量
                        Label lbl_spid = drv.FindControl("lbl_spid") as Label;//仓位ID
                       
                        DateTime out_storage_Time = DateTime.Now;

                        if (!string.IsNullOrEmpty(mids))
                        {
                            mid = Convert.ToInt32(mids);
                        }
                        if (!string.IsNullOrEmpty(out_storageList_ids))
                        {
                            out_storageList_id = Convert.ToInt32(out_storageList_ids);
                        }
                        if (!string.IsNullOrEmpty(lbl_spid.Text))
                        {
                            spid = Convert.ToInt32(lbl_spid.Text);
                        }
                        if (!string.IsNullOrEmpty(txt_out_storage_Time.Text))
                        {
                            out_storage_Time = Convert.ToDateTime(txt_out_storage_Time.Text);
                        }

                        //库存数量 
                        if (!string.IsNullOrEmpty(lbl_realCount.Text))
                        {
                            real_count = Convert.ToDecimal(lbl_realCount.Text);
                        }

                        #region 验证数量
                        try
                        {
                            if (!string.IsNullOrEmpty(txt_out_storage_count.Text))
                            {
                                out_count = Convert.ToDecimal(txt_out_storage_count.Text);
                            }
                            else
                            {
                                ClientScript.RegisterStartupScript(this.GetType(), "", "<script>alert('请填写出库数量！');</script>");
                            }

                        }
                        catch (Exception)
                        {
                            ClientScript.RegisterStartupScript(this.GetType(), "", "<script>alert('出库数量请填写数字！');</script>");
                        }



                        if (real_count - out_count < 0)
                        {
                            ClientScript.RegisterStartupScript(this.GetType(), "", "<script>alert('出库数量大于库存数量！');</script>");
                            return;
                        }
                        if (out_count > Convert.ToDecimal(this.lbl_notCount.Text))
                        {
                            ClientScript.RegisterStartupScript(this.GetType(), "", "<script>alert('出库数量大于未出库数量！');</script>");
                            return;
                        }
                     
                        #endregion


                        dzs.BLL.inventory venm = new BLL.inventory();

                        dzs.Model.inventory ven = venm.GetModel(inid);

                        dzs.BLL.out_storageList olm = new BLL.out_storageList();

                        dzs.Model.out_storageList ol = olm.GetModel(out_storageList_id);

                        if (ven != null && ol != null)
                        {
                            ven.nums = ven.nums - out_count;//更新库存表库存数量

                            ol.deal_status = "已出库";//更新出库单表状态为已出库

                            if (venm.Update(ven) && olm.Update(ol))
                            {
                                dzs.BLL.out_storage om = new BLL.out_storage();
                                dzs.Model.out_storage o = new Model.out_storage();

                                o.out_storage_count = out_count;
                                o.material_id = mid;
                                o.position_id = spid;
                                o.out_storage_Time = out_storage_Time;
                                o.out_storage_User_id = out_storage_User_id;
                                o.receive_address = receive_address;
                                o.receive_userName = receive_userName;
                                o.receive_userPhone = receive_userPhone;
                                o.out_storageList_id = out_storageList_id;
                                o.target_project = lbl_targetProject.Text;
                               
                             
                                if (om.Add(o) > 0)
                                {
                                    ClientScript.RegisterStartupScript(this.GetType(), "", "<script>alert('出库成功！');</script>");

                                    if (!string.IsNullOrEmpty(this.lbl_out_storageList_id.Text))
                                    {
                                        BindGV(Convert.ToInt32(this.lbl_out_storageList_id.Text));
                                    }
                                   
                                }



                            }
                        }
                    }
                    else
                    {
                        Response.Redirect("../Default.aspx");
                    }


                }
            }
        }

        protected void GridView1_PageIndexChanging(object sender, GridViewPageEventArgs e)
        {
            this.GridView1.PageIndex = e.NewPageIndex;

            if (Session["out_storageList_id"] != null)
            {
                int out_storageList_id = Convert.ToInt32(Session["out_storageList_id"]);

                BindGV(out_storageList_id);

            }
        }

        protected void GridView1_RowDataBound(object sender, GridViewRowEventArgs e)
        {
            if (e.Row.RowType ==  DataControlRowType.DataRow)
            {

                //DropDownList ddl_targetProject = e.Row.FindControl("ddl_targetProject") as DropDownList;

                //DataSet ds = new Maticsoft.BLL.project().GetList("");

                //ddl_targetProject.DataSource = ds;
                //ddl_targetProject.DataTextField = "projectName";
                //ddl_targetProject.DataValueField = "proid";

                //ddl_targetProject.DataBind();


                //Label lbl_proid = e.Row.FindControl("lbl_proid") as Label;
                //ddl_targetProject.SelectedValue = lbl_proid.Text;
            }
        }

       
       

       
    }
}